package com.yuyou.tas.lesson.service.impl;

import java.util.Date;
import java.util.List;

import com.yuyou.tas.lesson.mapper.LessonClassPlanChapterMapper;
import com.yuyou.tas.service.api.lesson.entity.LessonClassPlanChapter;
import com.yuyou.tas.util.bean.ResBoolSimpleInfo;
import com.yuyou.tas.util.enums.admin.AuthUserEnums;
import com.yuyou.tas.util.message.lesson.LessonClassMess;
import com.yuyou.tas.util.tool.DateUtil;
import com.yuyou.tas.util.tool.Sequence;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yuyou.tas.lesson.mapper.LessonClassUserRelMapper;
import com.yuyou.tas.lesson.service.ILessonClassUserRelService;
import com.yuyou.tas.service.api.lesson.entity.LessonClassUserRel;

/**
 * Created by Administrator on 2018/5/23.
 */
@Service
public class LessonClassUserRelServiceImpl implements ILessonClassUserRelService{

    @Autowired
    private LessonClassUserRelMapper lessonClassUserRelMapper;

    @Autowired
    private LessonClassPlanChapterMapper lessonClassPlanChapterMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return lessonClassUserRelMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(LessonClassUserRel record) {
    	record.setId(Sequence.getSequence().nextId());
    	record.setCreateTime(new Date());
        return lessonClassUserRelMapper.insert(record);
    }

    @Override
    public LessonClassUserRel selectByPrimaryKey(Long id) {
        return lessonClassUserRelMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(LessonClassUserRel record) {
        return lessonClassUserRelMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int deleteByLessonClassId(Long lessonClassId) {
        return lessonClassUserRelMapper.deleteByLessonClassId(lessonClassId);
    }

    @Override
    public ResBoolSimpleInfo<String> deleteByClassIdUserId(Long lessonClassId, Long authUserId, Integer type, Long lessonChapterId) {
        ResBoolSimpleInfo<String> res = new ResBoolSimpleInfo<>(true, null);

        // 主讲老师至少要一位
        if(type == AuthUserEnums.teacherType.lecturer.getValue()){
            List<LessonClassUserRel> teacherList = lessonClassUserRelMapper.findByLessonClassIdType(lessonClassId, AuthUserEnums.teacherType.lecturer.getValue());
            if(null != teacherList && teacherList.size() <= 1){
                res.setSuccess(false);
                res.setMessage(LessonClassMess.DELETE_FAILED);
                return res;
            }
        }

        // 删除主、代课老师
        if(type == AuthUserEnums.teacherType.substitute.getValue()){
            lessonClassPlanChapterMapper.setChapterTeacherNull(lessonClassId, lessonChapterId, authUserId);
            lessonClassUserRelMapper.deleteByClassIdUserIdChapterId(lessonClassId, authUserId, lessonChapterId);
        }else{
            lessonClassUserRelMapper.deleteByClassIdUserId(lessonClassId, authUserId);
        }
        return res;
    }

    @Override
	public List<LessonClassUserRel> findByUserId(Long userId) {
		return lessonClassUserRelMapper.findByUserId(userId);
	}

    @Override
    public ResBoolSimpleInfo<String> changeTeacher(Long lessonClassId, Long originTeacherId, Long destTeacherId, Integer type, Long lessonChapterId) {
        ResBoolSimpleInfo<String> res = new ResBoolSimpleInfo<>(true, null);
        // 替换老师没变，不做操作
        if(originTeacherId.equals(destTeacherId)){
            return res;
        }
        LessonClassUserRel destLessonClassUser = lessonClassUserRelMapper.selectByClassIdUserId(lessonClassId, destTeacherId);
        if(null != destLessonClassUser){
            res.setSuccess(false);
            res.setMessage(LessonClassMess.CLASS_TEACHER_EXIST);
            return res;
        }

        LessonClassUserRel originLessonClassUser;
        if(type == AuthUserEnums.teacherType.substitute.getValue()){
            originLessonClassUser = lessonClassUserRelMapper.selectByClassIdUserIdChapterId(lessonClassId, originTeacherId, lessonChapterId);
            LessonClassPlanChapter lessonClassPlanChapter = lessonClassPlanChapterMapper.selectByClassIdChapterIdTeacherId(lessonClassId, lessonChapterId, originTeacherId);
            if(null != lessonClassPlanChapter){
                LessonClassPlanChapter lessonClassPlanChapterUpdate = new LessonClassPlanChapter();
                lessonClassPlanChapterUpdate.setId(lessonClassPlanChapter.getId());
                lessonClassPlanChapterUpdate.setSubTeacher(destTeacherId);

                lessonClassPlanChapterMapper.updateByPrimaryKeySelective(lessonClassPlanChapterUpdate);
            }
        }else{
            originLessonClassUser = lessonClassUserRelMapper.selectByClassIdUserId(lessonClassId, originTeacherId);
        }
        LessonClassUserRel lessonClassUserRelUpdate = new LessonClassUserRel();
        lessonClassUserRelUpdate.setId(originLessonClassUser.getId());
        lessonClassUserRelUpdate.setAuthUserId(destTeacherId);
        lessonClassUserRelMapper.updateByPrimaryKeySelective(lessonClassUserRelUpdate);

        return res;
    }

    @Override
    public  ResBoolSimpleInfo<String> addTeacher(Long lessonClassId, Long authUserId, Integer type, Long lessonChapterId) {
        ResBoolSimpleInfo<String> res = new ResBoolSimpleInfo<>(true, null);
        LessonClassUserRel lessonClassUserRelAdd = new LessonClassUserRel();

        // 判断老师是否已存在班级
        if(type == AuthUserEnums.teacherType.lecturer.getValue()){
            LessonClassUserRel destLessonClassUser = lessonClassUserRelMapper.selectByClassIdUserId(lessonClassId, authUserId);
            if(null != destLessonClassUser){
                res.setSuccess(false);
                res.setMessage(LessonClassMess.CLASS_TEACHER_EXIST);
                return res;
            }
        }else{
            List<LessonClassUserRel> lessonClassUserList = lessonClassUserRelMapper.selectByClassIdUserIdType(lessonClassId, authUserId, AuthUserEnums.teacherType.lecturer.getValue());
            if(null != lessonClassUserList && lessonClassUserList.size() > 0){
                res.setSuccess(false);
                res.setMessage(LessonClassMess.CLASS_TEACHER_EXIST);
                return res;
            }
        }

        if(type == AuthUserEnums.teacherType.lecturer.getValue() ){
            List<LessonClassUserRel> teacherList = lessonClassUserRelMapper.findByLessonClassIdType(lessonClassId, AuthUserEnums.teacherType.lecturer.getValue());
            if(null != teacherList && teacherList.size() >= 2){
                res.setSuccess(false);
                res.setMessage(LessonClassMess.TEACHER_EXCEED_TWO);
                return res;
            }
            lessonClassUserRelAdd.setType(AuthUserEnums.teacherType.lecturer.getValue());
        }else {
            LessonClassPlanChapter lessonClassPlanChapter = lessonClassPlanChapterMapper.selectByClassIdChapterId(lessonClassId, lessonChapterId);
            if(null != lessonClassPlanChapter){
                lessonClassPlanChapter.setId(lessonClassPlanChapter.getId());
                lessonClassPlanChapter.setSubTeacher(authUserId);
                lessonClassPlanChapterMapper.updateByPrimaryKeySelective(lessonClassPlanChapter);
            }

            lessonClassUserRelAdd.setType(AuthUserEnums.teacherType.substitute.getValue());
            lessonClassUserRelAdd.setLessonClassPlanChapterId(lessonChapterId);
        }

        lessonClassUserRelAdd.setId(Sequence.getSequence().nextId());
        lessonClassUserRelAdd.setLessonClassId(lessonClassId);
        lessonClassUserRelAdd.setAuthUserId(authUserId);
        lessonClassUserRelAdd.setCreateTime(DateUtil.now());
        lessonClassUserRelMapper.insert(lessonClassUserRelAdd);

        return res;
    }

	@Override
	public int delByLesClaId7Type(Long lessonClassId, Integer type) {
		return lessonClassUserRelMapper.delByLesClaId7Type(lessonClassId, type);
	}

	@Override
	public int delBylesClaPlaChaId(Long lessonClassPlanChapterId) {
		return lessonClassUserRelMapper.delBylesClaPlaChaId(lessonClassPlanChapterId);
	}

	@Override
	public ResBoolSimpleInfo<String> addLessonClassUserRel(
			LessonClassUserRel record) {
		this.insert(record);
		return ResBoolSimpleInfo.getSuccessInfo();
	}
}
